package test.lyj.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import test.lyj.R;
import test.lyj.tool.Tool;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 带头像的列表
 * @title 
 * @author ysy
 * @date 2010-6-9
 * @email 1234gogogo@sina.com.cn
 */
public class ListAdapter extends BaseAdapter {
	// 数据
	private ArrayList<HashMap<String, Object>> dataMapList;
	private int layoutId;
	private LayoutInflater mInflater;
	private int[] componentIds;
	private String[] dates;
	private Context context;
	private Handler btnHandle;
	
	public ListAdapter(Context context,
			ArrayList<HashMap<String, Object>> map, int layoutId,
			String[] dates, int[] lids) {
		this.context = context;
		this.dataMapList = map;
		this.layoutId = layoutId;
		this.dates = dates;
		this.componentIds = lids;
		
		mInflater = LayoutInflater.from(context);
		
	}
	
	public ListAdapter(Context context,
			ArrayList<HashMap<String, Object>> map, int layoutId,
			String[] dates, int[] lids,Handler btnHandle) {
		this.context = context;
		this.dataMapList = map;
		this.layoutId = layoutId;
		this.dates = dates;
		this.componentIds = lids;
		this.btnHandle = btnHandle;
		
		mInflater = LayoutInflater.from(context);
		
	}
	
	@Override
	public int getCount() {
		return dataMapList.size();
	}


	
//	public Object getItem(int position) {
//
//		return dataMapList.get(position);
//	}
//	
	@Override
	public HashMap<String, Object> getItem(int position) {
		return dataMapList.get(position);
	}
	
	@Override
	public long getItemId(int position) {

		return position;
	}

	// ckList.put(holder.checkbox, d);

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			// 使用XML文件填充整个布局
			convertView = mInflater.inflate(layoutId, null);
			holder = new ViewHolder();// 生成存放数据的组件模�
			holder.icon = (ImageView) convertView.findViewById(componentIds[0]);
			holder.name= (TextView) convertView.findViewById(componentIds[1]);
			holder.address=(TextView) convertView.findViewById(componentIds[2]);
			holder.loadbtn=(Button) convertView.findViewById(R.id.item_btn);
			holder.imgbtn=(ImageView) convertView.findViewById(R.id.item_imgbtn);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}	
	
		
		OnClickListener itembtnL=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				holder.loadbtn.setText("...");
//				holder.loadbtn.setVisibility(View.GONE);
				/*Message message = new Message();
				message.what = 200;
				//Bundle b = new Bundle();
				//b.putString(Tool.TIMER_TASK_RESULT, "服务器连接中断,网络超时。");
				//message.setData(b);
				btnHandle.sendMessage(message);*/
				
			}
		};
		holder.loadbtn.setOnClickListener(itembtnL);
		
		//----------imageview onclicker
OnClickListener imgbtnL=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				holder.imgbtn.setBackgroundResource(R.drawable.botton_2);
//				holder.loadbtn.setVisibility(View.GONE);
				/*Message message = new Message();
				message.what = 200;
				//Bundle b = new Bundle();
				//b.putString(Tool.TIMER_TASK_RESULT, "服务器连接中断,网络超时。");
				//message.setData(b);
				btnHandle.sendMessage(message);*/
				
			}
		};
		holder.imgbtn.setOnClickListener(imgbtnL);
		/**
		 * 给行界面赋�?�?
		 */
		HashMap<String, Object> d = dataMapList.get(position);
		Object s = d.get(dates[0]);
			try {
				Log.d("listAdapter", "dates[0]"+dates[0]+"s:"+s);
//				Bitmap bm=(Bitmap) s;			
				holder.icon.setImageBitmap((Bitmap)d.get(dates[0]));
			} catch (Exception e) {
				
			}
		
		Object name = d.get(dates[1]);
		if (name != null)
			holder.name.setText(String.valueOf(name));
//		Bitmap ic = BitmapFactory.decodeResource(context.getResources(),
//				Integer.parseInt(String.valueOf(d.get(dates[0]))));

		Object address = d.get(dates[2]);
		if (address != null)
			holder.address.setText(String.valueOf(address));

		return convertView;
	}

	

	static class ViewHolder {
		TextView name;
		TextView address;
		ImageView icon;
		Button loadbtn; 
		ImageView imgbtn;
	}
	
	
//	/**
//	 * 得到指定行的数据
//	 * @param i
//	 * @return
//	 */
//	public HashMap<String ,Object> getChoiceBySortNum(int i){
//		return null;
//	}
	/**
	 * 改变指定行的数据为新数据
	 * @param postion
	 */
	public void changePostionValue(int position,HashMap<String, Object> newDate){
		HashMap<String, Object> olddate=(HashMap<String, Object>) getItem(position);
		Collection<String> changKeys=newDate.keySet();
		for (Iterator<String> iterator = changKeys.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			olddate.put(key, newDate.get(key));
		}
		notifyDataSetChanged();	
//	    	choiceNum=postion;
////			HashMap<String, Object> d = mapList.get(num);
////			d.put(dates[3], ck.isChecked());
//			for (Iterator<RadioButton> iterator = ckColl.keySet().iterator(); iterator
//					.hasNext();) {
//				RadioButton type = (RadioButton) iterator.next();
//				Object ob=ckColl.get(type);
//				if(ob!=null){
//				int v=Integer.valueOf(String.valueOf(ob));
//					if(v==postion)
//						type.setChecked(true);
//					else
//					 type.setChecked(false);
//				}
			}
	
//	public void setAllChoiceStatus(boolean flag){
//		for (Iterator<HashMap<String, Object>> iterator = dataMapList.iterator(); iterator
//		.hasNext();) {
//		HashMap<String, Object> m = (HashMap<String, Object>) iterator
//				.next();
//		m.put(dates[3], flag);
//	}
//		notifyDataSetChanged();	
//	}
}
